/*********************************************************************************************************************
**                          闁诲海鎳撻ˇ顖滅玻濡吋瀚婚梻鍫熺☉鐟欙箓鏌￠崒娑橆�滈柛蹇旑焽缁棃骞嬮悙鍙傛鏌￠崼婵愭█婵＄偓妞藉畷妤咁敆閳ь剙顕�?
**                          http://www.guibo.com.cn
**                               閻庤鎮堕崕鎶藉煝婵傜绠柨鐔剁矙瀵敻顢涘鍛厸缂備礁鑻悥鐓庘枍?
**
2-Mbit DataFlash (with Extra 64-Kbits), 1.65V Minimum
SPI Serial Flash Memory
256 bytes per page
264 bytes per page (default)
Page size can be factory pre-configured for 256 bytes
One SRAM data buffer (256/264 bytes)
 ____________________________________________________________________________________________________
| Each device has |   Each block has  |  Each sector has    |   Each page has    |                   |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      8M         |       64          |         4K          |        256         |       bytes       |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      32K        |       256         |         16          |                    |       pages       |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      2048       |       16          |                     |                    |       sectoss     |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      128        |                   |                     |                    |       blocks      |
|-----------------|-------------------|---------------------|--------------------|-------------------|
*********************************************************************************************************************/


#include "includes.h"


#if DFLASH_AT45_USE


Ex_DFLASH_Analog Ex_DFLASH_AnalogData;    //yhai20190207
uint16_t Ex_DFLASH__RwRecordTime;
uint8_t  VolPackageSerial=0,TempPackageSerial=0;
uint8_t  Ex_Dflash_ReadBuf[10];
uint16_t Ex_Dflash_Page=0;
uint64_t Ex_Dflash_DebugID;
uint16_t bottv_ms_ExDflash_Fre;

uint8_t  ExDflash_NOR_ChipEraseLockFlag=0;
uint16_t ExDflash_NOR_ChipErase_TimeCnt=10000;
uint8_t  ExDflash_NOR_ChipErase_EN_Flag;
uint8_t  ExDflash_NOR_DebugID_EN_Flag;
uint16_t LatestRunTimeMin=0;
uint8_t  BMS_DatRecordReadSelfLockFlag=0;
uint8_t  BMS_DatRecordReadFlag=0;


uint16_t ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
uint16_t ExDflash_RD_PageOffsetADD;
uint8_t  bottv_addr_DatRecodee=1U;      /*閻熸粎澧楅幐鍛婃櫠閻樿鎹堕柍銉闂佹悶鍎茬粙鎰版煂濠婂應鍋撳☉娅亪宕戝澶婄閺夌偞濯介崢顒勬煟閵娿儱顏╃紒顭戝弮瀹曪綁鏁�*/


#define T_DatRead  2//1


/*
********************************************************************************
**闂佸憡鍨兼慨銈夊汲閻旂厧瑙︾�广儱娉﹂悙鐑樻櫖婵繄顭秈d DatRecod_init(void)
**闂佸憡妫戦幏锟�    闂佺厧纾悺鏃堝几?
**闂佸憡鐟遍幏锟�    闂佽桨绀佹惔婊呮?
**闁哄鏅幏锟� 闂佹悶鍎婚幏锟� 闂佺锕﹂妴瀣?
**婵犮垼顔愰幏锟�    濠电偛顧�閹凤拷:
********************************************************************************
*/

uint8_t CalcCheckSum(uint8_t *PointerX,uint8_t CalNumL)
{
	uint8_t i,RVal = 0;
      for(i=0; i< CalNumL;i++)
      {
           RVal  += PointerX[i];
      }
      return RVal;
}



void DatRecod_init(void)
{
   //recover from ex ee
    uint16_t totalAS=0U;
    uint8_t  eedata[4U][4U];
    uint8_t  temp[2U];
    uint8_t  i,j;
    uint8_t  EEPROM_Data[4];

    /*read 4 buffers,start 0x0190U,increase by 4*/
    for(i=0;i<4;i++)
    {

    	exEEP_Read_Bytes(FRAM_DFLASH_ADDR, EEPROM_Data, 4);

        for(j=0U;j<4U;j++)
        {
            eedata[i][j]=EEPROM_Data[j];
        }
        Delay_ms(5U);
    }

    /*find the valid data*/
    bottv_addr_DatRecodee=255U;

    for(i=0U;i<4U;i++)
    {
        temp[0U]=eedata[i][0U]+eedata[i][1U];
        temp[1U]=0xff-temp[0U];//eedata[i][0U]+eedata[i][1U]+eedata[i][3U];

        if((temp[0U]==eedata[i][2U])&&(temp[1U]==eedata[i][3U]))
        {
            totalAS=eedata[i][0U];
            totalAS=totalAS<<8U;
            totalAS|=eedata[i][1U];


            if(totalAS!=0xFFFFU)
            {
                ExDflash_WR_PageOffsetADD= totalAS;
                bottv_addr_DatRecodee=i+1;
                if(bottv_addr_DatRecodee>=4U)
                    bottv_addr_DatRecodee=0U;
            }
        }
    }

    if(bottv_addr_DatRecodee==255U)
    {
        bottv_addr_DatRecodee=0U;
        ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
    }
}

/*
********************************************************************************
**闂佸憡鍨兼慨銈夊汲閻旂厧瑙︾�广儱娉﹂悙鐑樻櫖婵繄顭秈d ExDflashSendEraserSts(void)
**闂佸憡妫戦幏锟�    闂佺厧纾悺鏃堝几?
**闂佸憡鐟遍幏锟�    闂佽桨绀佹惔婊呮?
**闁哄鏅幏锟� 闂佹悶鍎婚幏锟� 闂佺锕﹂妴瀣?
**婵犮垼顔愰幏锟�    濠电偛顧�閹凤拷:
********************************************************************************
*/
void ExDflashSendEraserSts(void)
{
    stCAN_SWObj    FrameObj;  //闂佸搫绉村ú銈夊闯椤栫偛鏋侀柣妤�鐗嗙粊锕傛偨椤栥倖瀚�
    static uint16_t ExDflash_EraserTime;
	FrameObj.ulID.ulVal	=Ex_DlashSendInfID;//IDE,(DIR=TX)DATA Frame,8Data Length code
	FrameObj.ubLen  = 8 ;
	FrameObj.ubData[0] = 0xF1;// 闂佺懓灏呯粻鎾斥枍瀹ョ尣dex         DFlash Erasering
	if(ExDflash_EraserTime<=9)
	{
		ExDflash_EraserTime++;
		FrameObj.ubData[1] = 0x55;//濠殿喗绻愮徊钘夛耿椤忓牆绠奸柨鏇炲�归悵锟�
	}
	else
	{
		ExDflash_EraserTime=0;
		FrameObj.ubData[1] = 0xAA;//闂佺懓灏呯粻鎾斥枍鎼达絿纾奸柟鎯х摠鐏忥拷
		ExDflash_NOR_ChipEraseLockFlag=0;
	}
	FrameObj.ubData[2] = ExDflash_EraserTime;
	FrameObj.ubData[3] = 0;
	FrameObj.ubData[4] = 0;
	FrameObj.ubData[5] = 0;
	FrameObj.ubData[6] = 0;
	FrameObj.ubData[7]   =   (0<<7)|(0<<6)|(0<<5)|(0<<4)|0<<3|ExDflash_NOR_DebugID_EN_Flag<<2|BMS_DatRecordReadSelfLockFlag<<1|ExDflash_NOR_ChipEraseLockFlag;
	Debug_Send_Frame(&FrameObj);


}

/*
********************************************************************************
**闂佸憡鍨兼慨銈夊汲閻旂厧瑙︾�广儱娉﹂悙鐑樻櫖婵繄顭秈d CanRecExDflashProcess(uint8_t *pData)
**闂佸憡妫戦幏锟�    闂佺厧纾悺鏃堝几?
**闂佸憡鐟遍幏锟�    闂佽桨绀佹惔婊呮?
**闁哄鏅幏锟� 闂佹悶鍎婚幏锟� 闂佺锕﹂妴瀣?
**婵犮垼顔愰幏锟�    濠电偛顧�閹凤拷:
********************************************************************************
*/
 void CanRecExDflashProcess(uint8_t *pData)
 {

    if((pData[0] == 0xA0)&&(pData[1] == 0x0D)&&(pData[2] == 0xB2)&&(pData[3] == 0xC1)
           &&(pData[4] == 0xD0)&&(pData[5] == 0xBE)&&(pData[6] == 0xC6)&&(pData[7] == 0xAC)/*&&(bottv_flg_debug)*/)
    {
        ExDflash_NOR_ChipErase_EN_Flag=1; //A0	0D	B2	C1	D0	BE	C6	AC	闂佺绻堥崝搴∶鸿箛娑樼闁挎洖鍊归悵鐑Dflash Chip Erase time: 30s typical
    }

    else if ((pData[0] == 0xA0)&&(pData[1] == 0x0E)&&(pData[2] == 0xB6)&&(pData[3] == 0xC1)
           &&(pData[4] == 0x49)&&(pData[5] == 0x44)&&(pData[6] == 0xBA)&&(pData[7] == 0xC5)/*&&(bottv_flg_debug)*/)
    {
        ExDflash_NOR_DebugID_EN_Flag=1;//A0	0E	B6	C1	49	44	BA	C5	闁荤姴娲╅褑銇愰崶顒佸殟妞ゆ牗绮岄。绫孌   (闂佹悶鍎抽崑娑⑺�?xc84017)
    }

    else if((pData[0] == 0xA0)&&(pData[1] == 0x0F)&&(pData[2] == 0xB6)&&(pData[3] == 0xC1)
         &&(pData[4] == 0xCA)&&(pData[5] == 0xFD)&&(pData[6] == 0xBE)&&(pData[7] == 0xDD)/*&&(bottv_flg_debug)*/)
    {
        BMS_DatRecordReadFlag=1;       //A0	0F	B6	C1	CA	FD	BE	DD	闁荤姴娲╅褑銇愰崶顒�鏋侀柣妤�鐗嗙粊锕�菐閸ワ絽澧插ù锟�?
    }

    else
    {
        ;
    }

 }


/*
********************************************************************************
**闂佸憡鍨兼慨銈夊汲閻旂厧瑙︾�广儱娉﹂悙鐑樻櫖婵繄顭秈d BMS_DatRecordRead(void)
**闂佸憡妫戦幏锟�    闂佺厧纾悺鏃堝几閸栨彈S闁荤姴娲╅褑銇愰崶顒�鏋侀柣妤�鐗嗙粊锕傛偣娴ｈ绶茬紓锟�?
**闂佸憡鐟遍幏锟�    闂佽桨绀佹惔婊呮閻ㄥ紝id
**闁哄鏅幏锟� 闂佹悶鍎婚幏锟� 闂佺锕﹂妴瀣閻ㄥ紝id
**婵犮垼顔愰幏锟�    濠电偛顦介悡澶屾娴兼潙绠ユい鎰剁到娴煎酣鏌涘☉鏍у婵狅拷閿熺姵鏅搁柨锟�?
1闂侀潧妫斿鎺旂博閺夋垟鏋庨柍銉ㄥ皺閻熸劙鏌ｉ姀鐘典粵婵炴捁鍩栧鍕潩椤掞拷鐢帡鎮规担鍦憙妞ゆ洑鍗冲畷锝夊冀閿旂虎浼囧┑鐐叉閸撴繈寮查悩缁樷挄婵犲﹤妫崬鍓佹喐閻楀牊绀冮柡鍡欏枛楠炴垿顢氶崱娆戭槷
2闂侀潧妫斿ù鍥儍閻旂厧鐭楅柡鍫㈢┎s闂佽桨鑳舵晶妤�鐣垫担鐑樺闁哄娉曠粔鍧楁煛閸愵収鍟囩紒杈ㄧ箞瀹曟骞庨懞銉川cant婵炲濮鹃褎鎱ㄩ悢鐓庣闁哄牏鏁搁柧鍌炴煛娴ｅ搫顣肩�癸拷?
3闂侀潧妫旂粈浣虹礊婵狀毈lash闂佸搫鐗嗛ˇ浼村汲閻旂厧绠叉い鏃囧Г椤ρ囨煙缁嬫妲洪柛娆忛叄瀵偊鎮ч崼婵堛偊婵炲瓨绫傞崨顔芥闂佹眹鍔岀�氼剚鏅堕悩璇茶Е閹兼番鍔婇埀顒�瀛╅幆鏃囩疀閵壯咁槷闂佹眹鍨瑰畷顒傜礊鐎ｎ喖绀堢�广儱妫欏▓鍫曟煙鐠団�虫灈闁诡喗绮撳畷銏ゅ幢濡わ拷閽戝鏌℃担鍝勵暭鐎规挷鐒﹀濠氭嚍閵夈儱姣�
********************************************************************************
*/
//濠殿噯绲界换鎰拷鍨耿瀵偊鎮ч崼婵堛偊闂佸搫鐗為幏锟�36婵炴垶鎼╂禍锝夋儊閺嶎厼妫樻い鎾寸箑缁诲棝鏌熼銊﹀ 92婵炴垶鎼╂禍婊冪暦閻斿憡濯撮柟鎹愬紦缁诲棝鏌熼銊﹀ 23+2婵炴垶鎼╂禍婵囩┍椤栨稒鍎熼柨鏂裤仒缁诲棝鏌熼銊﹀  闂佺楠忛幏锟� 36+92*2+23+2=245 bytes
//6闂佸憡鐗曢幊鎾绘儊閺嶎厼妫樻い鎾跺枑濞堝爼鏌熼惂鍛婂//31闂佸憡鐗曢幊搴＄暦閻斿憡濯撮柟鎯х摠濞堝爼鏌熼惂鍛婂//3闂佸憡鐗曢幊蹇旂┍椤栨稒鍎熼柨锟�?/1闂佸憡鐗曢幊搴㈡叏閻愮儤鍊绘い鎾跺枍閸掑﹪骞栨潏楣冩闁哄棛鍠栭獮鎴︽晸?/婵炴垶鎼幏鐑芥煕韫囥儲瀚�41闂佸憡鐗曢幊蹇涘汲閻旂厧绠查柨锟�?

void BMS_DatRecordRead(void)
{
        stCAN_SWObj FrameObj;  //闂佸搫绉村ú銈夊闯椤栫偛鏋侀柣妤�鐗嗙粊锕傛偨椤栥倖瀚�
        uint16_t i=0,k=0;
        uint32_t Uint32ReadAdd;
        static uint16_t clccnt1=0, clccnt2=0;

//        static uint8_t RTCTimeArrayLast[6]={0x00,0x00,0x00,0x00,0x00,0x00};

        ExDflash_RD_PageOffsetADD=ExDflash_WR_PageOffsetADD;
        for(i=0;i<=ExDflash_TotalPage_Def;i++)
        {
////////////////////////////////////////////////////////////////////////////////////////////////////////
            if(clccnt1++>=500)
			{
				clccnt1=0;
				LED_Ctr_OVER();
			}

			if(get_SysIgnSts()==false)
            {
				 break;
			}
////////////////////////////////////////////////////////////////////////////////////////////////////////
            ExDflash_RD_PageOffsetADD--;//ExDflash_Page_Size;//濠殿噯绲界换鎴濐焽?56bit闂傚倵鍋撻柛顭戝枛椤旓拷
            if(ExDflash_RD_PageOffsetADD<ExDflash_StartPageAdd) ExDflash_RD_PageOffsetADD=ExDflash_EndPageAdd;  //ExDflash_Page_Size;//濠殿噯绲界换鎴濐焽?56bit闂傚倵鍋撻柛顭戝枛椤旓拷
            if(ExDflash_RD_PageOffsetADD>=ExDflash_StartPageAdd)
            {
                Ex_Dflash_Page=ExDflash_RD_PageOffsetADD-ExDflash_StartPageAdd;
            }
            else  Ex_Dflash_Page=ExDflash_RD_PageOffsetADD ;

            Uint32ReadAdd=((uint32_t)ExDflash_RD_PageOffsetADD)<<8;

            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0, 7);       //8
//            OS_EXIT_CRITICAL();
//            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[7], Uint32ReadAdd+60, 3);       //6
            OS_EXIT_CRITICAL();


            if((Ex_Dflash_ReadBuf[0]==0xff)||(Ex_Dflash_ReadBuf[1]==0xff)||(Ex_Dflash_ReadBuf[2]==0xff)||(Ex_Dflash_ReadBuf[3]==0xff)||(Ex_Dflash_ReadBuf[4]==0xff)||(Ex_Dflash_ReadBuf[5]==0xff) ||(Ex_Dflash_ReadBuf[6]==0xff)
             ||(Ex_Dflash_ReadBuf[0]==0x00)||(Ex_Dflash_ReadBuf[1]==0x00)||(Ex_Dflash_ReadBuf[2]==0x00)/*||(Ex_Dflash_ReadBuf[3]==0x00)||(Ex_Dflash_ReadBuf[4]==0x00)||(Ex_Dflash_ReadBuf[5]==0x00)*/ ||(Ex_Dflash_ReadBuf[6]<0x01)||(Ex_Dflash_ReadBuf[6]>0x04)
			             /*楠烇拷                                           					 閺堬拷 							閺冿拷						                   閺冿拷								閸掞拷								 缁夛拷                                                                  	                    vewmv_enum_workingMode*/
			 ||(0xff!=CalcCheckSum(&Ex_Dflash_ReadBuf[0],8))
			 ||(0xff!=CalcCheckSum(&Ex_Dflash_ReadBuf[8],2))
			   /*瑜版捁顕伴崣鏍ㄦ闂傜繝璐�0xff閹达拷0x00 閹存牞锟藉懏顥呮宀勬晩鐠囷拷 閸掓瑩锟斤拷閸戦缚顕氶崠鍛殶閹诡喚娈戠拠璇插絿*/
//  			 ||((ExDflash_RD_PageOffsetADD<ExDflash_TotalPage_Def)&&(Ex_Dflash_ReadBuf[0]==RTCTimeArrayLast[0])&&(Ex_Dflash_ReadBuf[1]==RTCTimeArrayLast[1])&&(Ex_Dflash_ReadBuf[2]==RTCTimeArrayLast[2])&&(Ex_Dflash_ReadBuf[3]==RTCTimeArrayLast[3])&&(Ex_Dflash_ReadBuf[4]==RTCTimeArrayLast[4])&&(Ex_Dflash_ReadBuf[5]==RTCTimeArrayLast[5]))
			  )
            {
               continue;
            }

//            OS_ENTER_CRITICAL();
//            for(i=0;i<6;i++)
//            {
//            	RTCTimeArrayLast[i]=Ex_Dflash_ReadBuf[i];
//            }
//            OS_EXIT_CRITICAL();


            if(clccnt2++>=1)
			{
				clccnt2=0;
				LED_Ctr_OVER();
			}


            FrameObj.ulID.ulVal	= Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            FrameObj.ubData[0]=0x01;//闂佸憡鐗曢幊搴ゃ亹?
            FrameObj.ubData[1]=Ex_Dflash_Page/256;//婵＄偑鍊濆褑銇�?
            FrameObj.ubData[2]=Ex_Dflash_Page%256;//婵＄偑鍊濆褑銇�?
            for(k=0;k<5;k++)
            {
                FrameObj.ubData[k+3] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);
            vTaskDelay(2);


            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5, 7);           //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x02;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);


            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7, 7);       //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x03;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }

            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);

            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7, 7);       //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x04;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }

            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);

            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x05;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);

            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x06;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x07;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //閹兼潙绻愰崹顏堝矗閿�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x08;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //閹兼潙绻愰崹顏堝矗閿�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x09;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //閹兼潙绻愰崹顏堝矗閿�?
            }
//            FrameObj.ubData[4]=Ex_Dflash_ReadBuf[4];//bottv_num_max7823AliveCnt;
//            FrameObj.ubData[5]=Ex_Dflash_ReadBuf[6];//bottv_num_bmu1AliveCnt
//            FrameObj.ubData[6]= (uint8_T)((ExDflash_RD_PageOffsetADD-ExDflash_StartPageAdd)>> 8);
//            FrameObj.ubData[7]= (uint8_T)((ExDflash_RD_PageOffsetADD-ExDflash_StartPageAdd) >> 0);
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            VolPackageSerial=0;
            while(VolPackageSerial<=17)
            {
                if(VolPackageSerial<17)
                {
                    FrameObj.ubLen  = 8 ;
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+VolPackageSerial*7, 7);  //7濞戞搩浜滃畷鐔告媴閿�?
                    OS_EXIT_CRITICAL();
                    FrameObj.ubData[0] = 0x10+VolPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];
                    FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];
                    FrameObj.ubData[3] = Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = Ex_Dflash_ReadBuf[4];
                    FrameObj.ubData[6] = Ex_Dflash_ReadBuf[5];
                    FrameObj.ubData[7] = Ex_Dflash_ReadBuf[6];
                }
                else if(VolPackageSerial==17)
                {
                    FrameObj.ubLen  = 8 ;
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+VolPackageSerial*7, 4);  //4濞戞搩浜滃畷鐔告媴閿�?
                    OS_EXIT_CRITICAL();
                    FrameObj.ubData[0] = 0x10+VolPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];
                    FrameObj.ubData[2] = 0xff;//Ex_Dflash_ReadBuf[1];
                    FrameObj.ubData[3] = 0xff;//Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = 0xff;//Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = 0xff;//0xff;
                    FrameObj.ubData[6] = 0xff;//0xff;
                    FrameObj.ubData[7] = 0xff;//0xff;
                }
                Debug_Send_Frame(&FrameObj);    //vcan
                vTaskDelay(2);
                VolPackageSerial++;
            }
/////////////////////////////////////////48婵炴垶鎼╂禍婵囩┍椤栨稒鍎熼柨鏇氱贰閸ゃ垽鏌涘▎娆愬//////////////////////////////////////////////////////////////////////////
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            TempPackageSerial=0;
            while(TempPackageSerial<=6)
            {
                if(TempPackageSerial<6)
                {
                    FrameObj.ubLen  = 8 ;
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+120+TempPackageSerial*7, 7);
                    OS_EXIT_CRITICAL();
                    FrameObj.ubData[0] = 0xA0+TempPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];
                    FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];
                    FrameObj.ubData[3] = Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = Ex_Dflash_ReadBuf[4];
                    FrameObj.ubData[6] = Ex_Dflash_ReadBuf[5];
                    FrameObj.ubData[7] = Ex_Dflash_ReadBuf[6];
                }
                else if(TempPackageSerial==6)
                {
                    FrameObj.ubLen  = 8 ;
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+120+TempPackageSerial*7, 6);
                    OS_EXIT_CRITICAL();
                    FrameObj.ubData[0] = 0xA0+TempPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];
                    FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];
                    FrameObj.ubData[3] = Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = Ex_Dflash_ReadBuf[4];
                    FrameObj.ubData[6] = Ex_Dflash_ReadBuf[5];
                    FrameObj.ubData[7] = 0xff;
                }

                Debug_Send_Frame(&FrameObj);//vcan
                vTaskDelay(2);
                TempPackageSerial++;
            }

/////////////////////////////////////////2婵炴垶鎼╂禍婊勬叏閻愮儤鍊绘い鎾拷铏濠电偞鎸撮弲娑氾拷鐟扮－閹风姷鎷犻幓鎺旀偧//////////////////////////////////////////////////////////////////////////

//            FrameObj.ulID.ulVal	=	Ex_DlashSendInfID ;//0x6A0 ;//IDE,(DIR=TX)DATA Frame,8Data Length code
//            FrameObj.ubLen  = 3 ;
//            OS_ENTER_CRITICAL();
//            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+40+144+48, 2);  //2婵炴垶鎼幏锟�
//            OS_EXIT_CRITICAL();
//            FrameObj.ubData[0] = 0xE0+1;
//            FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];
//            FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];
            //FrameObj.ubData[3] = 0;
            //FrameObj.ubData[4] = 0;
            //FrameObj.ubData[5] = 0;
            //FrameObj.ubData[6] = 0;
            //FrameObj.ubData[7] = 0;
           // Debug_Send_Frame(&FrameObj);    //vcan
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//            vTaskDelay(10);
        }
}

/*
********************************************************************************
**闂佸憡鍨兼慨銈夊汲閻旂厧瑙︾�广儱娉﹂悙鐑樻櫖婵炲棛鈷搕eRecordTask
**闂佸憡妫戦幏锟�    闂佽壈娅曢弸鑽ゆ?
**闂佸憡鐟遍幏锟�    闂佽桨绀佹惔婊呮?
**闁哄鏅幏锟� 闂佹悶鍎婚幏锟� 闂佺锕﹂妴瀣?
**婵犮垼顔愰幏锟�    濠电偛顦介悡澶屾?
********************************************************************************
*/
void DatRecordProcess(void)
 {
     static uint8_t ID_timeout=0;

	   if(ExDflash_NOR_ChipErase_TimeCnt<10000)ExDflash_NOR_ChipErase_TimeCnt++;
       if((ExDflash_NOR_ChipErase_EN_Flag)&&(ExDflash_NOR_ChipErase_TimeCnt>6000)&&(!BMS_DatRecordReadSelfLockFlag)&&(!ExDflash_NOR_ChipEraseLockFlag))//
       {
            ExDflash_NOR_ChipEraseLockFlag=1; //婵炴垶鎸搁敃锕傚极閸涘﹦鈻旈柨鐔绘閳诲酣鍩勯崘鈺�绮繝銏ｅ煐閼归箖宕㈤幋鐘冲闂傦拷閹邦喚鎲﹀┑鐐叉閸撴繈骞楀┑瀣挃闁匡拷?
            ExDflash_NOR_ChipErase_TimeCnt=0;        //闁哄鏅濋崑鐔兼偨婵犳艾绠肩�广儱瀚粙濠傗槈閹惧磭孝缂佹儼浜禍姝岀疀鎼达綀澹�2闂佸憡甯掑Λ婵嬪箰?
            ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
//            OS_ENTER_CRITICAL();
            ExDflash_NOR_ChipErase(); //eraser whole chip
//            OS_EXIT_CRITICAL();
            ExDflash_NOR_ChipErase_EN_Flag=0; //闂傚倸娴勯幏鐑芥偡閺囨氨鍔嶉柡鍌欑窔瀹曠兘濡搁妷褎婢栭梻鍌氬閹冲繘骞楀┑瀣挃闁靛牆鎳庡В鎰版煛娓氾拷椤ｏ妇绮崨瀛橆棃闁匡拷?
            if(SaveFlag.sb.DatRecod==0)
            {
                SaveFlag.sb.DatRecod=1U;
            }
       }

        if((BMS_DatRecordReadFlag)&&(!BMS_DatRecordReadSelfLockFlag)&&(bottv_ms_sysRunTime>3000)) //yhai20170703//婵炴垶鎼幏閿嬬箾閸″繆鍋撻懠顒傛啰闂佹眹鍨归悘姘�掗懜闈涚窞婵﹩鍓欑敮鎺楁偣娴ｅ湱鎽冩い鏇氬嵆瀹曪綁寮介敂缁紘濠电偛妫岄崜婵嬪疾閻樼粯鈷曟繝濠傛－閸炲墎鎲搁悧鍫熺闁哄棛鍠栭獮鎴︻敋閸℃瑧顦梺鍛婅壘閺堫剙顪冮崒鐐茬闁规儼濮ら敍鏀僡nt婵炲濮鹃褎鎱ㄩ悢鐓庢瀬闁绘鐗嗙粊锟�
        {
            BMS_DatRecordReadSelfLockFlag=1;
            BMS_DatRecordRead();
            BMS_DatRecordReadFlag=0;  //闂傚倸娴勯幏鐑芥偡閺囨氨鍔嶉柡鍌欑窔瀹曠兘濡搁妷褎婢栭梻鍌氬閹虫捇顢氭导鏉戠煑闁哄诞鍐╃槪闂佽桨绶氶。锔剧箔閸涘瓨顥堥柨锟�?
            BMS_DatRecordReadSelfLockFlag=0;//闂備焦褰冮敃銉╁棘娓氾拷楠烇拷闁煎鍊曞▓鐘绘煛娴ｅ搫顣肩�癸拷?
        }

        if(ExDflash_NOR_DebugID_EN_Flag)
        {
            OS_ENTER_CRITICAL();
            Ex_Dflash_DebugID=ExDflash_NOR_ReadID();  //0x1f23000100
            OS_EXIT_CRITICAL();
            ExDflash_NOR_DebugID_EN_Flag=0;
            ID_timeout=0;
        }

        if(ID_timeout++>0)
        {
        	ID_timeout=3;
        	Ex_Dflash_DebugID=0;
        }




 }
/*
********************************************************************************
**闂佸憡鍨兼慨銈夊汲閻旂厧瑙︾�广儱娉﹂悙鐑樻櫖婵繄顭秈d DateRecordTask(void *pdata)
**闂佸憡妫戦幏锟�    闂佽壈娅曢弸鑽ゆ?
**闂佸憡鐟遍幏锟�    闂佽桨绀佹惔婊呮?
**闁哄鏅幏锟� 闂佹悶鍎婚幏锟� 闂佺锕﹂妴瀣?
**婵犮垼顔愰幏锟�    濠电偛顦介悡澶屾? 50ms
********************************************************************************
*/
void DataRecordStore(void)
{
	uint8_t   i=0;
	static uint16_t  resistance=50000;
	uint32_t  Uint32WriteAdd;
	static uint16_t  ExDflash_Save_WR_Add_Cnt=600;
	static uint8_t  ExDflash_Save_WR_Add_ProtectCnt=0;
	uint32_t Time_DatRecord=600;
//	static uint8_t ExDflashflag=0;

	resistance=bottv_ohm_isoRpRes<bottv_ohm_isoRnRes ? bottv_ohm_isoRpRes:bottv_ohm_isoRnRes;
//////////////////////////////////////////////////package1//////////////////////////////////////////////////////////////////////////////////////////////
	//PageNum   len=2
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 0] =  RealTime.year;//
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 1] =  RealTime.month;//
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 2] =  RealTime.day; //
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 3] =  RealTime.hour;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 4] =  RealTime.minute;
///////////////////////////////////////////////////package2/////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 5] =  RealTime.second;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 6] =  vewm_enum_workingMode;//vewm_enum_workingMode;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 7] =  fltm_enum_FltLv;//cantv_enum_BMS_FaultLevel;//fltm_enum_FltLv;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 8] =  (uint8_T)(soce_pct_soc >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 9] =  (uint8_T)(soce_pct_soc >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[10] =  (uint8_T)((uint16_T)((real_T)bottv_v_packVlt * 0.001) >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[11] =  (uint8_T)((uint16_T)((real_T)bottv_v_packVlt * 0.001) >> 0);
///////////////////////////////////////////////////package3/////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[12] =  (uint8_T)((uint16_T)((real_T)bottv_ma_cur * 0.001) >> 8);  //闂佹眹鍨归悘姘舵儓濠婂牆绠戦柤濮愬�栭弳鈺傜箾鏉堢偓瀚�    gpio_ma_Curr
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[13] =  (uint8_T)((uint16_T)((real_T)bottv_ma_cur * 0.001) >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[14] =  (uint8_T)(bottv_mv_p12V >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[15] =  (uint8_T)(bottv_mv_p12V >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[16] =  (uint8_T)(resistance >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[17] =  (uint8_T)(resistance >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[18]=  bottv_no_cVltMax;
	 ///////////////////////////////////////////////////package4/////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[19]=  (uint8_T)(bottv_mv_cVltMax >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[20]=  (uint8_T)(bottv_mv_cVltMax >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[21]=  bottv_no_cVltMin ;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[22]=  (uint8_T)(bottv_mv_cVltMin >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[23]=  (uint8_T)(bottv_mv_cVltMin >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[24]=  (uint8_T)(bottv_mv_cellVltAvg >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[25]=  (uint8_T)(bottv_mv_cellVltAvg >> 0);
//////////////////////////////////////////////////package5//////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[26]=  (uint8_T)0;//(cvltv_mv_cVltDif >> 8);  //voldiff
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[27]=  (uint8_T)0;//(cvltv_mv_cVltDif >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[28]=   bottv_c_cTmpMax;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[29]=   bottv_no_cTmpMax;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[30]=   bottv_c_cTmpMin;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[31]=   bottv_no_cTmpMin;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[32]=   bottv_c_cTmpAvg;
//////////////////////////////////////////////////package6//////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[33]=   bottv_k_cpRatio;//ctemv_mv_cTempDif;    //tempdiff
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[34]=   bottv_mv_SccVolt/20;// (uint8_T)(LatestRunTimeMin >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[35]=   bottv_mv_QccVolt/20;//(uint8_T)(LatestRunTimeMin >> 0);

//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[36]=   	 cvltv_flg_cVltOV1<<7
//																|cvltv_flg_cVltUV1<<6
//																|hvmmv_flg_packOVFlt1<<5
//																|hvmmv_flg_packUVFlt1<<4
//																|currv_flg_curOC1<<3
//																|currv_flg_chgcurOC1<<2
//																|currv_flg_schgcurOC1<<1
//																|ctemv_flg_cTmpOT1;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[37]=  	 ctemv_flg_cTmpUT1<<7
//																|ctemv_flg_chgCTmpOT1<<6
//																|ctemv_flg_chgCTmpUT1<<5
//																|cvltv_flg_cVltDif1<<4
//																|ctemv_flg_cTmpDif1<<3
//																|bottv_flg_IsoISuperSts1<<2
//																|socmv_flg_socLowLimit1<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[38]=  	 0<<7
//																|0<<6
//																|0<<5
//																|0<<4
//																|0<<3
//																|0<<2
//																|0<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[39]=   	 cvltv_flg_cVltOV2<<7
//																|cvltv_flg_cVltUV2<<6
//																|hvmmv_flg_packOVFlt2<<5
//																|hvmmv_flg_packUVFlt2<<4
//																|currv_flg_curOC2<<3
//																|currv_flg_chgcurOC2<<2
//																|currv_flg_schgcurOC2<<1
//																|ctemv_flg_cTmpOT2;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[40]=  	 ctemv_flg_cTmpUT2<<7
//																|ctemv_flg_chgCTmpOT2<<6
//																|ctemv_flg_chgCTmpUT2<<5
//																|cvltv_flg_cVltDif2<<4
//																|ctemv_flg_cTmpDif2<<3
//																|bottv_flg_IsoISuperSts2<<2
//																|socmv_flg_socLowLimit2<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[41]=   	 0<<7
//																|0<<6
//																|0<<5
//																|0<<4
//																|0<<3
//																|0<<2
//																|0<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[42]=   	 cvltv_flg_cVltOV3<<7
//																|cvltv_flg_cVltUV3<<6
//																|hvmmv_flg_packOVFlt3<<5
//																|hvmmv_flg_packUVFlt3<<4
//																|currv_flg_curOC3<<3
//																|currv_flg_chgcurOC3<<2
//																|currv_flg_schgcurOC3<<1
//																|ctemv_flg_cTmpOT3;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[43]=   	 ctemv_flg_cTmpUT3<<7
//																|ctemv_flg_chgCTmpOT3<<6
//																|ctemv_flg_chgCTmpUT3<<5
//																|cvltv_flg_cVltDif3<<4
//																|ctemv_flg_cTmpDif3<<3
//																|bottv_flg_IsoISuperSts3<<2
//																|socmv_flg_socLowLimit3<<1
//																|bottv_flg_vltSampLineFault;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[44]=   	 bottv_flg_tmplinefault<<7
//																|bottv_flg_heatRlyAdhFault<<6
//																|bottv_flg_posRlyAdhFault<<5
//																|precv_flg_precChgLoseFlt<<4
//																|bottv_flg_HeatTimeoutFault<<3
//																|bottv_flg_SOCJumpError<<2
//																|canrv_flg_VMSComFlt<<1
//																|canrv_flg_cmComFlt;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[45] =	 0<<7
//																|0<<6
//																|0<<5
//																|0<<4
//																|0<<3
//																|0<<2
//																|0<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[46]=  	 0<<7
//																|0<<6
//																|0<<5
//																|0<<4
//																|0<<3
//																|0<<2
//																|0<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[47]=  	 	 bottv_flg_VIgn<<7
//																|bottv_flg_SIgn<<6
//																|bottv_flg_QIgn<<5
//																|0<<4
//																|0<<3
//																|0<<2
//																|0<<1
//																|0;
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[48]=   (uint8_T)0;//(bottv_ah_BMS_SingleChgCap >> 8);//SingleChgeCap_AH
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[49]=   (uint8_T)0;//(bottv_ah_BMS_SingleChgCap >> 0);
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[50]=   (uint8_T)0;//(bottv_ah_BMS_SingleDChgCap >> 8);  //SingleDChgCap_AH
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[51]=   (uint8_T)0;//(bottv_ah_BMS_SingleDChgCap >> 0);//
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[52]=   (uint8_T)0;//(bottv_ah_BMS_TotalChgCap >> 24);//TotalChgCap_AH
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[53]=   (uint8_T)(bottv_ah_BMS_TotalChgCap >> 16);//
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[54]=  (uint8_T)(bottv_ah_BMS_TotalChgCap >> 8);//
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[55]=  (uint8_T)(bottv_ah_BMS_TotalChgCap >> 0);
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[56]=  (uint8_T)(bottv_ah_BMS_TotalDchgCap >> 24);//TotalDchgCap_AH
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[57]=  (uint8_T)(bottv_ah_BMS_TotalDchgCap >> 16);
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[58]=  (uint8_T)(bottv_ah_BMS_TotalDchgCap >> 8);
//	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[59]=  (uint8_T)(bottv_ah_BMS_TotalDchgCap >> 0);
//////////////////////////////////娴犮儰绗呴弫鐗堝祦娑撳窋ack1閸嬫瓭heck///////BMSInfCheckDat///////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[60]=   0xFF-CalcCheckSum(&Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[0],7 );//BMSInfCheckDat;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[61]=   0;//bottv_num_Ltc6815AliveCnt;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[62]=   0xFF-0;//bottv_num_Ltc6815AliveCnt;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[63]=   bottv_num_bmu1AliveCnt;
	for(i=0;i<120;i++)         //144婵炴垶鎼╂禍婊冪暦閻斿憡濯撮柨锟�?
	{


		Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[i]=bottv_mv_cellVolt[i]>=2000?((bottv_mv_cellVolt[i]-2000)/10):0x00;//((2000-bottv_mv_CellVol[i])/10);

	}

	for(i=0;i<48;i++)         //48
	{
		Ex_DFLASH_AnalogData.Ex_DFLASH_TempDat_Array[i]   = bottv_c_cellTemp[i]!=0?bottv_c_cellTemp[i]:0xff;
	}

//	for(i=0;i<2;i++)         //
//	{
//		Ex_DFLASH_AnalogData.Ex_DFLASH_HTempDat_Array[i]   = 0;//bottc_mv_heatTemp[i];
//	}


	 if(get_SysIgnSts()==true)
	 {

		 if((!ExDflash_NOR_ChipEraseLockFlag)&&(!BMS_DatRecordReadSelfLockFlag)) //闂侀棿璀﹂崹铏櫠閺嶃劎鈻旂�广儱鎳忛煬顒勬煙閸栵紕鐣垫繛鍛叄閹晠鎳滅喊澶嬪闁匡拷?
		{
			Ex_DFLASH__RwRecordTime++;
			if(fltm_enum_FltLv>=4)         Time_DatRecord = T_DatRecord(2);
			else if(fltm_enum_FltLv>=3)    Time_DatRecord = T_DatRecord(4);
			else if(fltm_enum_FltLv>=2)    Time_DatRecord = T_DatRecord(6);
			else if(fltm_enum_FltLv>=1)    Time_DatRecord = T_DatRecord(10);
			else                           Time_DatRecord = T_DatRecord(60);

			if(Ex_DFLASH__RwRecordTime>=Time_DatRecord)  //濠殿喗绻愮徊浠嬫偉閸洖绫嶉柣妯挎珪濞呭﹪姊婚崨顔剧煁婵★拷?0S闁荤姳鐒﹀妯肩礊瀹ュ棛鈻旈柨鐔绘閳诲酣鍩ラ崨顖滎槷閻熸粎澧楅幐椋庣博鐎靛摜妫憸搴ㄥ疾閻樼粯鈷曟繝濠傚暟閹界娀鏌涢敂鍝勫婵＄偛鍊块弫宥呯暆閸愭儳鏁归悷婊呭濞叉牠骞婇崱娑樺珘闁绘梹鍎肩粈锟�5S婵炴垶鎼幏閿嬬箾閸″繑顫夌紒杈ㄧ箖缁傚秴鐣濋埀顒侇殽閸ヮ剙鏋侀柛娑樼摠椤斿洭鏌￠崘顓у晣缂佽鲸绻勯幏瀣级鐠恒劎协闂佸憡绋忛崝宥咃耿?S婵炴垶鎼幏閿嬬箾閸″繑顫夌紒杈ㄧ箖缁嬪寰勫畝鍕礈闂佽桨绀侀幊妯衡枔娴兼潙绫嶉柟顖涘濞堝爼鏌℃担铏圭瘈閻狅拷?S婵炴垶鎼幏閿嬬箾閸犫�冲簻閹风兘鏁�?
														   //1闁诲繐绻愮换鎴濐渻閸屾粍濯奸柡澶庢硶缁夛拷360婵＄偑鍊ч幏锟� 闂佺楠忛幏锟�32768婵＄偑鍊ч幏锟�, 闁诲孩绋掕摫鐎殿啫鍥х闁跨喍绮欓幃浠嬪Ω閿旂瓔妲梻鍌氬�荤换婵堟嫻?2768/360=91.2h
			{
				Ex_DFLASH__RwRecordTime=0;
				Uint32WriteAdd=((uint32_t) ExDflash_WR_PageOffsetADD)<<8;
				if(ExDflash_WR_PageOffsetADD % 0x10==0)  //濠殿噯绲界换瀣煂濞撶剳ctor 4K缂備礁鏈钘壩涢崸妤佸剭闁告洦鍓氶幉褰掓⒒閸曡埖瀚�
				{
//					 OS_ENTER_CRITICAL();
//					 ExDflash_NOR_SectorErase(Uint32WriteAdd);  // 1page=256bytes      1sector = 16page     1block = 16sector     1chip =128block
//					 OS_EXIT_CRITICAL();
					if(SaveFlag.sb.DatRecod==0)
					{
						SaveFlag.sb.DatRecod=1U;
					}
				}
//				 OS_ENTER_CRITICAL();
				 ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[0],Uint32WriteAdd+0, 64U);        //闁糕晝鍎ゅ﹢鐗堢┍閳╁啩绱�36濞戞搫鎷�
//				 OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[0],Uint32WriteAdd+64, 60U);      //闁告娲戠紞瀣礈閿�?2濞戞搫鎷�
//			    OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[60],Uint32WriteAdd+64+60, 60U);   //闁告娲戠紞瀣触閿�?2濞戞搫鎷�
//			    OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_TempDat_Array[0],Uint32WriteAdd+64+60+60, 48U);  //婵炴挴鏅涚��?8濞戞搫鎷�
//			    OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
//				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH__HTempDat_Array[0],Uint32WriteAdd+39+72+72+48, 2U);   //闂佸憡姊绘慨鐑藉磿瑜版帗鍤夋繝濠傚閸掑﹪骞栨潏鐐2婵炴垶鎼幏锟� // 36+92+92+23+2=245
//				OS_EXIT_CRITICAL();

				ExDflash_WR_PageOffsetADD++;// ExDflash_Page_Size;//濠殿噯绲界换鎴濐焽?56bit闂傚倵鍋撻柛顭戝枛椤旓拷
				if(ExDflash_WR_PageOffsetADD>ExDflash_EndPageAdd) ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
			}
		}
   }
   else    //婵炴垶鎸搁鍥极?
   {
	   //SPI2_SetMode(0);
	    ExDflash_WR_PageOffsetADD &=ExDflash_EndPageAdd;    //32767
		if((ExDflash_Save_WR_Add_Cnt++>=600)&&(ExDflash_Save_WR_Add_ProtectCnt<2))    //婵炴垶鎸搁鍥极閹间礁绠柡宓唭妤呮煛閸剚瀚规繝銏ｅ煐閻喚绮担绯曟瀻闁炽儴灏欑粻浠嬫倵濞戣櫕瀚�
		{
			ExDflash_Save_WR_Add_Cnt=0;
			ExDflash_Save_WR_Add_ProtectCnt++;
			if(SaveFlag.sb.DatRecod==0)
			{
				SaveFlag.sb.DatRecod=1U;
			}
		}

   }
}

void DatRecodWrite(void)
{
    uint8_t SysSocDataBuf[2];
    uint8_t sohTemp[2U];
    uint8_t res=0U;
    uint16_t tempSoh;
    uint8_t  EEPROM_Data[4];

	tempSoh=ExDflash_WR_PageOffsetADD;

	for(res=0U;res<3U;res++)
	{
		/*write as*/
		SysSocDataBuf[0]=(uint8_t)(tempSoh>>8U);
		SysSocDataBuf[1]=(uint8_t)(tempSoh>>0U);
		OS_ENTER_CRITICAL();
		exEEP_Write_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U, SysSocDataBuf, sizeof(SysSocDataBuf));		//婵烇絽娲︾换鍌炴偤?
		OS_EXIT_CRITICAL();
		Delay_ms(6);
		OS_ENTER_CRITICAL();
		exEEP_Read_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U, EEPROM_Data, 4);
		OS_EXIT_CRITICAL();
		if((EEPROM_Data[0]==SysSocDataBuf[0])&&(EEPROM_Data[1]==SysSocDataBuf[1]))
		{
			break;
		}
	}


	sohTemp[0U]=SysSocDataBuf[0U]+SysSocDataBuf[1U];
	sohTemp[1U]=0xff-sohTemp[0U];


	SysSocDataBuf[0U]=sohTemp[0U];
	SysSocDataBuf[1U]=sohTemp[1U];


	if(res<3U)
	{
		for(res=0U;res<3U;res++)
		{
			OS_ENTER_CRITICAL();
			exEEP_Write_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U+2U, SysSocDataBuf, sizeof(SysSocDataBuf));		//婵烇絽娲︾换鍌炴偤?
			OS_EXIT_CRITICAL();
			Delay_ms(6);
			OS_ENTER_CRITICAL();
			exEEP_Read_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U+2U, EEPROM_Data, 4);
			OS_EXIT_CRITICAL();
			if((EEPROM_Data[0]==SysSocDataBuf[0])&&(EEPROM_Data[1]==SysSocDataBuf[1]))
			{
				bottv_addr_DatRecodee++;
				if(bottv_addr_DatRecodee>=4U)
					bottv_addr_DatRecodee=0U;
				break;
			}
		}
	}
}



/*****************************************************************************
* Date:	2018-12-07
* By:	xzhang
* Func:	闂佽桨鑳舵晶妤�鐣垫担琛″亾濞戞顏堝磻瀹ュ棛顩烽悹鍥ㄥ絻椤拷(5ms婵炲濮鹃褎鎱ㄩ悢鐓庣鐟滄垿銆�?
*****************************************************************************/
void App_DataRecordTask(void)
{
	TickType_t xNextWakeTime;

	/* Initialise xNextWakeTime - this only needs to be done once.*/
	xNextWakeTime = xTaskGetTickCount();

	for(;;)
	{

		/////////////////////////////////////////////////////////////////////////////////
//			returnValue[13]=(int32_t)uxTaskGetStackHighWaterMark(NULL);
		/////////////////////////////////////////////////////////////////////////////////

		DatRecordProcess();
		DataRecordStore();
		vTaskDelayUntil(&xNextWakeTime, DatRecordTASK_PERIOD / portTICK_PERIOD_MS );
	}
}



#endif



